package com.clhbolg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clhbolg.domain.Job;
import com.clhbolg.domain.bo.JobBo;
import com.clhbolg.domain.vo.JobVo;
import com.clhbolg.mapper.JobMapper;
import com.clhbolg.service.IJobService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 定时任务调度Service业务层处理
 *
 * @author cailinhu
 * @date 2023-02-08
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class JobServiceImpl implements IJobService {

    private final JobMapper baseMapper;

    /**
     * 查询定时任务调度
     */
    @Override
    public JobVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询定时任务调度列表
     */
    @Override
    public TableDataInfo<JobVo> queryPageList(JobBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Job> lqw = buildQueryWrapper(bo);
        Page<JobVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询定时任务调度列表
     */
    @Override
    public List<JobVo> queryList(JobBo bo) {
        LambdaQueryWrapper<Job> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Job> buildQueryWrapper(JobBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Job> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getInvokeTarget()), Job::getInvokeTarget, bo.getInvokeTarget());
        lqw.eq(StringUtils.isNotBlank(bo.getCronExpression()), Job::getCronExpression, bo.getCronExpression());
        lqw.eq(bo.getMisfirePolicy() != null, Job::getMisfirePolicy, bo.getMisfirePolicy());
        lqw.eq(bo.getConcurrent() != null, Job::getConcurrent, bo.getConcurrent());
        lqw.eq(bo.getStatus() != null, Job::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增定时任务调度
     */
    @Override
    public Boolean insertByBo(JobBo bo) {
        Job add = BeanUtil.toBean(bo, Job.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改定时任务调度
     */
    @Override
    public Boolean updateByBo(JobBo bo) {
        Job update = BeanUtil.toBean(bo, Job.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Job entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除定时任务调度
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
